package product

import (
	"context"
	"gitee.com/zyw0605688_admin/gozero/product_service/internal/svc"
	"gitee.com/zyw0605688_admin/gozero/product_service/internal/types"
	"gitee.com/zyw0605688_admin/gozero/product_service/model"
	"strconv"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductAddLogic) ProductAdd(req *types.ProductAddRequest) (resp *types.ProductAddResponse, err error) {
	item := model.Product{
		ProductName:  req.ProductName,
		ProductPrice: req.ProductPrice,
	}
	err = l.svcCtx.DB.Save(&item).Error
	if err != nil {
		logx.Error("插入数据库失败：", err)
		return nil, err
	}
	resp = &types.ProductAddResponse{
		Id:           strconv.Itoa(int(item.ID)),
		ProductName:  item.ProductName,
		ProductPrice: item.ProductPrice,
	}
	return
}
